package com.yanchangapp.ui.activity.purchase;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.yanchangapp.R;
import com.yanchangapp.adapter.fragment.PurchaseListFgAdapter;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.bean.FilterBean;
import com.yanchangapp.model.bean.NotifyBean;
import com.yanchangapp.model.bean.TabBean;
import com.yanchangapp.ui.fragment.purchase.PListFragment;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.widget.ViewPagerFixed;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

/**
 * 采购订单列表页面....
 *
 * 现在考虑,订单需要修改的情况....
 *
 *
 * 创建/修改订单成功/作废订单/提交审批成功等等
 *
 */
public class POrderListActivity extends BaseActivity {

    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.tv_publish_now)
    TextView tvPublishNow;
    @BindView(R.id.btnCreate)
    Button btnCreate;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPagerFixed viewPager;

    private PurchaseListFgAdapter mPurchaseListFgAdapter;
    private List<TabBean> mTabBeans;
    private List<PListFragment> mListFragments;
    public static void show(Activity activity) {
        Intent intent = new Intent(activity, POrderListActivity.class);
        activity.startActivity(intent);
    }
    private int currentSelection = 0;
    public static String REFRESH_LIST = "REFRESH_LIST";
    private String keywords = null;
    private String startTime = null;
    private String endTime  = null;

    public static final String KEYWORDS = "keyword";
    public static final String START_TIME = "startTime";
    public static final String END_TIME = "endTime";

    public static final String NOTIFY_FILTER_DATA = "notify_filter_data";
    @Override
    protected void init() {
        // 初始化数据
        mTabBeans = new ArrayList<>();
        mListFragments = new ArrayList<>();
        mListFragments.clear();
        mTabBeans.clear();
        mTabBeans.add(new TabBean("全部",null));
        mTabBeans.add(new TabBean("待提交","0"));
        mTabBeans.add(new TabBean("待审批","1"));
        mTabBeans.add(new TabBean("已通过","2"));
        mTabBeans.add(new TabBean("已发送收货通知","3"));
        mTabBeans.add(new TabBean("订单已入库","7"));
        mTabBeans.add(new TabBean("已驳回","4"));
        mTabBeans.add(new TabBean("已销毁","8"));
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_porder_list;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvPublishNow.setVisibility(View.VISIBLE);
        tvPublishNow.setText("筛选");
        tvToolbarTitle.setText("采购订单列表");

        for (int i=0;i<mTabBeans.size();i++){
            mListFragments.add(createPListFragment(mTabBeans.get(i)));
        }
        mPurchaseListFgAdapter = new PurchaseListFgAdapter(getSupportFragmentManager(),mListFragments,mTabBeans);

        viewPager.setAdapter(mPurchaseListFgAdapter);
        viewPager.setCurrentItem(0);
        viewPager.setOffscreenPageLimit(mListFragments.size());
        tabLayout.setupWithViewPager(viewPager);
    }

    private PListFragment createPListFragment(TabBean tabBean){
        PListFragment pListFragment = new PListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(PListFragment.TITLE,tabBean.getTitle());
        bundle.putString(PListFragment.VALUE,tabBean.getParams());
        pListFragment.setArguments(bundle);
        return pListFragment;
    }

    @Override
    protected void initData() {
        mRxManager.on(REFRESH_LIST, new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                justRefresh();
            }
        });
    }

    public void hideCreateBtn(){
        btnCreate.setVisibility(View.GONE);
    }

    public void showCreateBtn(){
        btnCreate.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initListener() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                // 当前选中哪个Fragment....
                currentSelection = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick({R.id.ivToolbarNavigation, R.id.tv_publish_now, R.id.btnCreate})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivToolbarNavigation:
                finish();
                break;
            case R.id.tv_publish_now:
                // 跳转活动....
                POrderFilterActivity.show(POrderListActivity.this,keywords,startTime,endTime);
                break;
            case R.id.btnCreate:
                POrderPathActivity.show(POrderListActivity.this);
                break;
        }
    }

    /**
     * 模拟刷新
     */
    private void justRefresh() {
        PListFragment fragment = mListFragments.get(currentSelection);
        fragment.handleRefresh();
    }

    /**
     * 跳转活动进行传值
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            if (requestCode == AppConstant.RECODE_FILTER_ATY){
                keywords =  data.getStringExtra(KEYWORDS);
                startTime = data.getStringExtra(START_TIME);
                endTime = data.getStringExtra(END_TIME);
                // 通知数据改变
                FilterBean filterBean = new FilterBean(keywords,startTime,endTime,mTabBeans.get(currentSelection).getTitle());
                mRxManager.post(NOTIFY_FILTER_DATA,filterBean);
            }
        }
    }
}
